//
//  OSCEmoticonViewController.m
//  OSCEmoctionTools
//
//  Created by 8b410-2 on 17/1/6.
//  Copyright © 2017年 abb. All rights reserved.
//

#import "OSCEmoticonViewController.h"
#import "OSCEmoticonCollectionView.h"
#import "OSCEmoticonPackage.h"

typedef void(^DidClickEmoticonBlock)(OSCEmoticon *emoticon);
@interface OSCEmoticonViewController () <UICollectionViewDelegate,UICollectionViewDataSource,UIScrollViewDelegate>

@property (nonatomic, strong) UIPageControl *pageControl;

@property (nonatomic, strong) OSCEmoticonCollectionView *collectionView;

@property (nonatomic, strong) NSArray<OSCEmoticonPackage *> *packages;


@property (nonatomic, strong)  DidClickEmoticonBlock didClickEmoticonBlock;
@end

@implementation OSCEmoticonViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)setupUI {
    [self.view addSubview:self.collectionView];
    [self.view addSubview:self.pageControl];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    self.pageControl.translatesAutoresizingMaskIntoConstraints = false;
    self.collectionView.translatesAutoresizingMaskIntoConstraints = false;
    
    NSMutableArray *cons = [[NSMutableArray alloc] init];
    NSDictionary *viewDict = @{@"collectionView" : self.collectionView,
                               @"pageControl" : self.pageControl};
    [cons addObjectsFromArray:[NSLayoutConstraint
                     constraintsWithVisualFormat:@"H:|-0-[collectionView]-0-|"
                     options:0
                     metrics:nil
                     views:viewDict]];
    [cons addObjectsFromArray:[NSLayoutConstraint
                     constraintsWithVisualFormat:@"H:|-0-[pageControl]-0-|"
                     options:0
                     metrics:nil
                     views:viewDict]];
    [cons addObjectsFromArray:[NSLayoutConstraint
                     constraintsWithVisualFormat:@"V:|-0-[collectionView]-0-[pageControl(15)]-0-|"
                     options:0
                     metrics:nil
                     views:viewDict]];
    [self.view addConstraints:cons];
    
}

- (void)didSelectEmoticonBlock:(void (^)(OSCEmoticon *))block {
    _didClickEmoticonBlock = block;
}
- (NSInteger)horizontalItemFromVerticalItem:(NSInteger)item {
    NSInteger remainder = item % emoticonMaxCountPerPage;
    return (remainder/ emoticonMaxRows) + (remainder % emoticonMaxRows) * emoticonMaxCols + item / 24 * 24;
}
- (UIPageControl *)pageControl {
    if (!_pageControl) {
        _pageControl = [UIPageControl new];
        [_pageControl setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
        [_pageControl setCurrentPage:0];
        NSInteger numberOfPages = self.packages[0].emoticons.count % emoticonMaxCountPerPage == 0 ? self.packages[0].emoticons.count / emoticonMaxCountPerPage : self.packages[0].emoticons.count / emoticonMaxCountPerPage + 1;
        _pageControl.numberOfPages = numberOfPages;
    }
    return _pageControl;
}

- (OSCEmoticonCollectionView *)collectionView {
    if (!_collectionView) {
        _collectionView = [[OSCEmoticonCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:[[OSCEmoticonLayout alloc] init]];
    }
    return _collectionView;
}

- (NSArray *)packages {
    if (!_packages) {
        _packages = [OSCEmoticonPackage sharePackageList];
    }
    return _packages;
}

#pragma mark - UICollectionViewDataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return self.packages.count - 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.packages[section].emoticons.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    OSCEmoticonCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:OSCEmoticonCollectionViewReuseId forIndexPath:indexPath];
    OSCEmoticonPackage *package = self.packages[indexPath.section];

    OSCEmoticon *emoticon = package.emoticons[[self horizontalItemFromVerticalItem:indexPath.item]];
    cell.emoticon = emoticon;
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    OSCEmoticon *emoticon = self.packages[indexPath.section].emoticons[[self horizontalItemFromVerticalItem:indexPath.item]];
    if (_didClickEmoticonBlock) {
        _didClickEmoticonBlock(emoticon);
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    self.pageControl.currentPage = scrollView.contentOffset.x / scrollView.bounds.size.width;
}

@end
